
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.IOException;
import java.util.Collections;

public class ObjectList {

    //Superclass er n�dt til at have loadObjectFromFile, hvis loadAll og save skal virke. 
    //Men da loadObject f�rst specificeres i subclasses har jeg lavet en private "dummy"-method.
    public DataObject loadObjectFromFile(String dataObject_String) throws FileNotFoundException {
        Coach dummyCoach = new Coach("N/A", "N/A", "N/A");
        return dummyCoach;
    }

    public ArrayList<DataObject> loadAllObjectsFromFile(String fileName) throws FileNotFoundException {
        Scanner inFile = new Scanner(new File(fileName));
        ArrayList<DataObject> objectList = new ArrayList<DataObject>();
        objectList.clear();
        while (inFile.hasNextLine()) {
            String dataObject_String = inFile.nextLine();
            if (dataObject_String.length() > 0) {
                    objectList.add(this.loadObjectFromFile(dataObject_String));
            }
        }
        return objectList;
    }

    public void saveObject(String fileName, DataObject dataObject) throws FileNotFoundException {
        ArrayList<DataObject> allObjects = new ArrayList<DataObject>();
        allObjects.clear();
        for (int i = 0; i < loadAllObjectsFromFile(fileName).size(); i++) {
            allObjects.add(loadAllObjectsFromFile(fileName).get(i));
        }
        allObjects.add(dataObject);
        PrintStream outstream = new PrintStream(new File(fileName));
        for (int i = 0; i < allObjects.size(); i++) {
            outstream.println(allObjects.get(i));
        }
    } 
//        public void saveObject(String fileName, DataObject dataObject) throws FileNotFoundException {
//        ArrayList<String> data = new ArrayList<String>();
//        Scanner memberReader = new Scanner(new File(fileName));
//
//        while (memberReader.hasNext()) {
//            data.add(memberReader.next());
//        }
//        data.add(dataObject.toString());
//        ArrayList<String> result = new ArrayList<String>();
//        Collections.sort(data);
//        for (int i = 1; i < data.size(); i++){
//            if (!data.get(i).equals(data.get(i-1))){
//                result.add(data.get(i));
//            }
//        }
//        PrintStream outstream = new PrintStream(new File(fileName));
//        for (int j = 0; j < result.size(); j++){
//            outstream.println(result.get(j));
//        }
//    }
}